package com.example.springboot2.Control;

import com.alibaba.fastjson.JSONObject;
import com.example.springboot2.CommonHelper.RedisHelper;
import com.example.springboot2.DB_Helper.DataBase;
import com.example.springboot2.DB_Helper.JDBCUtils;
import com.example.springboot2.Model.Sys_Module;
import com.example.springboot2.Model.Sys_User;
import com.example.springboot2.Service.Sys_ModuleService;
import com.example.springboot2.ViewModel.menu;
import com.example.springboot2.ViewModel.roleAuth;
import com.fasterxml.jackson.databind.Module;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/Sys_Module")
public class Sys_ModuleController extends BaseController {
    @Resource
    private RedisHelper redisHelper=new RedisHelper();
    private Sys_ModuleService sys_moduleService=new Sys_ModuleService();
    private DataBase<Sys_Module> database=new DataBase<Sys_Module>();

    @PostMapping("/GetListByUserId")
    public ReturnListObject GetListByUserId()
            throws SQLException, InstantiationException, IllegalAccessException {
        ReturnListObject result=new ReturnListObject();
        result.Code=200;
        String token=request.getHeader("token");
        Sys_User user=(Sys_User) redisHelper.get(token);
        List<Sys_Module> ModuleList=sys_moduleService.GetModuleByUser(user);

        List<menu> menus=new ArrayList<>();
        List<Sys_Module> FirstModuleList=ModuleList.stream().filter(x->x.Sys_ModuleParentId.equals("")).collect(Collectors.toList());
        for (Sys_Module module:
                FirstModuleList) {
            menu temp=new menu();
            temp.icon=module.Sys_ModuleIcon;
            temp.name=module.Sys_ModuleName;
            temp.url=module.Sys_ModuleUrl;
            temp.ModuleId=module.Sys_ModuleId;
            temp.subMenus=sys_moduleService.GetSubMenuList(module.Sys_ModuleId,ModuleList);

            menus.add(temp);
        }
        result.Result=menus;
        return result;
    }
    @PostMapping("/GetListAll")
    public ReturnListTable GetListAll()throws SQLException, InstantiationException, IllegalAccessException
    {
        ReturnListTable result=new ReturnListTable();
        result.code=0;
        List<Sys_Module> ModuleList=sys_moduleService.GetModuleList();
        result.data=ModuleList;
        result.count= ModuleList.size();
        return result;
    }
    @PostMapping("/SaveModule")
    public  ReturnString SaveModule(@RequestParam()String KeyValue,@RequestParam()String StrEntity) throws IllegalAccessException {
        ReturnString result=new ReturnString();
        result.Code=200;
        Sys_Module module=  JSONObject.parseObject(StrEntity,Sys_Module.class);
        String sql="";
        if(KeyValue.isEmpty())
        {
            module.Sys_ModuleId= UUID.randomUUID().toString();
            sql=database.add(module);
        }
        else
        {
            sql=database.update(module);
        }
        Connection conn = JDBCUtils.findConnection();
        PreparedStatement pre = null;
        ResultSet res = null;
        try {
            pre = conn.prepareStatement(sql);
            pre.execute();
            result.Message="保存成功";
            return  result;

        } catch (SQLException e) {
            result.Message=e.toString();
            //e.printStackTrace();
        }
        return result;
    }
    @PostMapping("/GetListByRoleId")
    public ReturnListTable GetListByRoleId(@RequestParam()String RoleId) throws SQLException, InstantiationException, IllegalAccessException {
        ReturnListTable Result=new ReturnListTable();
        Result.code=200;
        List<Sys_Module> ModuleList=sys_moduleService.GetModuleList();
        List<Sys_Module> HasModuleList=sys_moduleService.GetModuleListByRoleId(RoleId);

        List<roleAuth> roleAuthList=new ArrayList<roleAuth>();
        for (Sys_Module module:
                ModuleList) {
            roleAuth temp=new roleAuth();
            temp.name=module.Sys_ModuleName;
            temp.pId=module.Sys_ModuleParentId;
            temp.id=module.Sys_ModuleId;
            temp.open=true;
            boolean IsHas = HasModuleList.stream().anyMatch(x -> x.Sys_ModuleId.equals(module.Sys_ModuleId));
            if(IsHas)
            {
                temp.checked=true;
            }
            else
            {
                temp.checked=false;
            }
            roleAuthList.add(temp);
        }
        Result.data=roleAuthList;
        return Result;
    }
}
